//
//  CGSchoolViewController.m
//  SchoolClient
//
//  Created by mac on 15/10/27.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGSchoolViewController.h"

@interface CGSchoolViewController ()

@property(nonatomic,strong)UIButton *areaButton;              //区域选择
@property(nonatomic,strong)UIButton *schoolButton;            //高校选择
@property(nonatomic,strong)UIButton *timeButton;              //招聘时间
@property(nonatomic,assign)CGFloat btSize;
@property(nonatomic,strong)NSMutableArray *products;           //所有产品

@property(nonatomic,strong)NSArray *areaFilters;               //区域选择
@property(nonatomic,strong)NSArray *schoolFilters;             //高校选择
@property(nonatomic,strong)NSArray *timeFilters;               //时间
@property(nonatomic,strong)UITableView *areaTableView;         //区域选择弹出框
@property(nonatomic,strong)UITableView *schoolTableView;       //高校选择弹出框
@property(nonatomic,strong)UITableView *timeTableView;         //招聘时间弹出框

@property(nonatomic,strong)UIWindow *searchWindow;
@property(nonatomic,strong)UIView *shadeView;
@property(nonatomic,strong)CGSearchBar *searchBar;
@property(nonatomic,strong)UIButton *cancleButton;
@property(nonatomic,strong)UITableView *searchTableView;       //搜索结果弹出框
@property(nonatomic,strong)NSArray *searchResults;             //搜索结果数据存储
@property(nonatomic,strong)NSString *selectedArea;             //用户选择的区域

@property(nonatomic,assign)BOOL areaCancle;
@property(nonatomic,assign)BOOL schoolCancle;
@property(nonatomic,assign)BOOL timeCancle;

@property(nonatomic,assign)BOOL isFirstLoad;                   //是否第一次进入该页面

@end

@implementation CGSchoolViewController


-(NSMutableArray *)products{
    if (!_products) {
        _products = [NSMutableArray array];
    }
    return _products;
}

-(NSArray *)searchResults{
    if (!_searchResults) {
        _searchResults = [NSArray array];
    }
    return _searchResults;
}

-(NSArray *)timeFilters{
    if (!_timeFilters) {
        _timeFilters = @[@"取消选择",@"一天",@"三天",@"一周",@"一月"];
    }
    return _timeFilters;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    //添加导航栏右边搜索按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"navigationbar_search_white"] style:UIBarButtonItemStylePlain target:self action:@selector(popSearchView)];
    self.areaCancle = YES;
    self.schoolCancle = YES;
    self.timeCancle = YES;
    
    self.btSize = 14;
    [self initHeaderView];
    [self initTableView];
    [self initAreaTableView];
    [self initSchoolTableView];
    [self initTimeTableView];
    [self initSearchTableView];
    
    //添加下拉刷新控件
    [self.tableView addHeaderWithTarget:self action:@selector(loadNewStatus)];
    
    //自动下拉刷新
    [self.tableView headerBeginRefreshing];
    
    //上拉加载旧数据
    [self.tableView addFooterWithTarget:self action:@selector(loadMoreStatus)];
    
    //加载区域选择的数据
    [self fetchAreaFilters];
    //加载高校选择的数据
    [self fetchSchoolFilters];
    
    //使cell的线条充满
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.areaButton setTitle:@"区域选择" forState:UIControlStateNormal];
    [self.schoolButton setTitle:@"高校选择" forState:UIControlStateNormal];
    [self.timeButton setTitle:@"招聘时间" forState:UIControlStateNormal];
    
    if (self.isFirstLoad) {
        self.isFirstLoad = NO;
        return;
    }
    
    [self.products removeAllObjects];
    [self loadNewStatus];
    self.selectedArea = nil;
}

-(void)initHeaderView{
    
    //区域选择Button
    CGRect aFrame = CGRectMake(0, 0, SCREEN_WIDTH/3, 44);
    self.areaButton = [[UIButton alloc]initWithFrame:aFrame];
    [self.areaButton setTitle:@"区域选择" forState:UIControlStateNormal];
    [self.areaButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.areaButton.titleLabel.font = [UIFont systemFontOfSize:self.btSize];
    [self.areaButton setImage:[UIImage imageNamed:@"enterprise_arrow_down"] forState:UIControlStateNormal];
    //[self.areaButton setImage:[UIImage imageNamed:@"school_arrow_up"] forState:UIControlStateSelected];
    self.areaButton.imageEdgeInsets = UIEdgeInsetsMake(0, SCREEN_WIDTH/3 - 10, 0, 0);
    self.areaButton.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    self.areaButton.tag = 101;
    [self.view addSubview:self.areaButton];
    //添加点击事件
    [self.areaButton addTarget:self action:@selector(popOverView:) forControlEvents:UIControlEventTouchUpInside];
    
    //高校选择Button
    aFrame.origin.x += SCREEN_WIDTH/3;
    self.schoolButton = [[UIButton alloc]initWithFrame:aFrame];
    [self.schoolButton setTitle:@"高校选择" forState:UIControlStateNormal];
    [self.schoolButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.schoolButton.titleLabel.font = [UIFont systemFontOfSize:self.btSize];
    [self.schoolButton setImage:[UIImage imageNamed:@"enterprise_arrow_down"] forState:UIControlStateNormal];
    //[self.schoolButton setImage:[UIImage imageNamed:@"school_arrow_up"] forState:UIControlStateSelected];
    self.schoolButton.imageEdgeInsets = UIEdgeInsetsMake(0, SCREEN_WIDTH/3 - 10, 0, 0);
    self.schoolButton.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    self.schoolButton.tag = 102;
    self.schoolButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    [self.view addSubview:self.schoolButton];
    //添加点击事件
    [self.schoolButton addTarget:self action:@selector(popOverView:) forControlEvents:UIControlEventTouchUpInside];

    //招聘时间Button
    aFrame.origin.x += SCREEN_WIDTH/3;
    self.timeButton = [[UIButton alloc]initWithFrame:aFrame];
    [self.timeButton setTitle:@"招聘时间" forState:UIControlStateNormal];
    [self.timeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.timeButton.titleLabel.font = [UIFont systemFontOfSize:self.btSize];
    [self.timeButton setImage:[UIImage imageNamed:@"enterprise_arrow_down"] forState:UIControlStateNormal];
    //[self.timeButton setImage:[UIImage imageNamed:@"school_arrow_up"] forState:UIControlStateSelected];
    self.timeButton.imageEdgeInsets = UIEdgeInsetsMake(0, SCREEN_WIDTH/3 - 10, 0, 0);
    self.timeButton.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    self.timeButton.tag = 103;
    [self.view addSubview:self.timeButton];
    //添加点击事件
    [self.timeButton addTarget:self action:@selector(popOverView:) forControlEvents:UIControlEventTouchUpInside];
    
    //中间2条竖条
    for (int i = 0; i < 2; i ++) {
        CGRect bFrame = CGRectMake(SCREEN_WIDTH/3 * (i+1), 10, 1, 24);
        UIView *lineView = [[UIView alloc]initWithFrame:bFrame];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:lineView];
    }
    
}

#pragma mark 添加表格
-(void)initTableView{
    
    CGRect aRect = CGRectMake(0,44,SCREEN_WIDTH,SCREEN_HEIGHT-64-44-49);
    self.tableView = [[UITableView alloc] initWithFrame:aRect  style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview: self.tableView];
}


-(void)initAreaTableView{
    
    CGRect bRect = CGRectMake(0,44,SCREEN_WIDTH,SCREEN_HEIGHT-64-44-49);
    self.areaTableView = [[UITableView alloc] initWithFrame:bRect  style:UITableViewStylePlain];
    self.areaTableView.delegate = self;
    self.areaTableView.dataSource = self;
    self.areaTableView.backgroundColor = [Tools getColor:@"ebebeb"];
    [self.view addSubview: self.areaTableView];
    self.areaTableView.hidden = YES;            //默认隐藏弹出框
    
}

-(void)initSchoolTableView{
    
    CGRect bRect = CGRectMake(0,44,SCREEN_WIDTH,SCREEN_HEIGHT-64-44-49);
    self.schoolTableView = [[UITableView alloc] initWithFrame:bRect  style:UITableViewStylePlain];
    self.schoolTableView.delegate = self;
    self.schoolTableView.dataSource = self;
    self.schoolTableView.backgroundColor = [Tools getColor:@"ebebeb"];
    [self.view addSubview: self.schoolTableView];
    self.schoolTableView.hidden = YES;            //默认隐藏弹出框
    
}

-(void)initTimeTableView{
    
    CGRect bRect = CGRectMake(0,44,SCREEN_WIDTH,SCREEN_HEIGHT-64-44-49);
    self.timeTableView = [[UITableView alloc] initWithFrame:bRect  style:UITableViewStylePlain];
    self.timeTableView.delegate = self;
    self.timeTableView.dataSource = self;
    self.timeTableView.backgroundColor = [Tools getColor:@"ebebeb"];
    [self.view addSubview: self.timeTableView];
    self.timeTableView.hidden = YES;            //默认隐藏弹出框
    
}

-(void)initSearchTableView{
    
    CGRect bRect = CGRectMake(0,44,SCREEN_WIDTH,SCREEN_HEIGHT-64-44-49);
    self.searchTableView = [[UITableView alloc] initWithFrame:bRect  style:UITableViewStylePlain];
    self.searchTableView.delegate = self;
    self.searchTableView.dataSource = self;
    self.searchTableView.backgroundColor = [Tools getColor:@"ebebeb"];
    [self.view addSubview: self.searchTableView];
    self.searchTableView.hidden = YES;            //默认隐藏弹出框
    
}


#pragma mark 弹出搜索框
-(void)popSearchView{
    
    [self.tableView reloadData];
    self.areaTableView.hidden = YES;
    self.schoolTableView.hidden = YES;
    self.timeTableView.hidden = YES;
    self.searchTableView.hidden = YES;
    
    self.areaButton.selected = NO;
    self.schoolButton.selected = NO;
    self.timeButton.selected = NO;
    
    self.shadeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.shadeView.backgroundColor = [UIColor blackColor];
    self.shadeView.alpha = 0.5;
    self.searchWindow = [UIApplication sharedApplication].keyWindow;
    [self.searchWindow addSubview:self.shadeView];
    
    self.searchBar = [[CGSearchBar alloc]initWithFrame:CGRectMake(20, 64+44, SCREEN_WIDTH - 100, 35)];
    self.searchBar.placeholder = @"查找公司或职位";
    self.searchBar.returnKeyType = UIReturnKeySearch;
    self.searchBar.delegate = self;
    //[self.searchBar becomeFirstResponder];
    [self.searchWindow addSubview:self.searchBar];
    
    self.cancleButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 75, 64+44, 55, 35)];
    [self.cancleButton setBackgroundImage:[UIImage imageNamed:@"login_very_again_btn_bg"] forState:UIControlStateNormal];
    [self.cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancleButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.searchWindow addSubview:self.cancleButton];
    
    [self.cancleButton addTarget:self action:@selector(hidSearchView) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark 单击取消按钮隐藏搜索框
-(void)hidSearchView{
    
    [self.searchBar resignFirstResponder];
    self.shadeView.hidden = YES;
    self.searchBar.hidden = YES;
    self.cancleButton.hidden = YES;
    
}

#pragma mark 单击回车键开始搜索
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.searchBar resignFirstResponder];
    self.shadeView.hidden = YES;
    self.searchBar.hidden = YES;
    self.cancleButton.hidden = YES;
    self.searchTableView.hidden = NO;
    
    [self searchWithKeyword:textField.text];
    
    return YES;
}

#pragma mark 放大镜搜索结果
-(void)searchWithKeyword:(NSString *)keyword{
    
    //2.发送登录请求
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"page"] = @"0";
    parameters[@"pageSize"] = [NSString stringWithFormat:@"%d",(int)SCREEN_HEIGHT/100];
    parameters[@"search"] = keyword;
    
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/school/getData.app" parameters:parameters success:^(id responseData) {
        
        NSLog(@"搜索请求发送成功");
        NSDictionary *jsonDic =  [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        NSString *value = jsonDic[@"data"];
        if (value) {
            NSData *valueData = [value dataUsingEncoding:NSUTF8StringEncoding];
            self.searchResults = [CGInfoSchool objectArrayWithJSONData:valueData];
        }else{
            self.searchResults = nil;
        }
        
        [self.searchTableView reloadData];
        self.searchTableView.hidden = NO;
        NSLog(@"result:%@",value);
        
    } failure:^(NSError *error) {
        
        NSLog(@"搜索请求发送失败！");
    }];
}

#pragma mark 筛选框弹出事件
-(void)popOverView:(UIButton *)sender{
    
    //self.searchTableView.hidden = YES;
    
    switch (sender.tag) {
        
        case 101:
            [self fetchAreaFilters];
            self.areaTableView.hidden = !self.areaTableView.hidden;
            self.schoolTableView.hidden = YES;
            self.timeTableView.hidden = YES;
            break;
            
        case 102:
            [self fetchSchoolFilters];
            self.schoolTableView.hidden = !self.schoolTableView.hidden;
            self.areaTableView.hidden = YES;
            //self.schoolTableView.hidden = YES;
            self.timeTableView.hidden = YES;
            break;
        
        default:
            self.timeTableView.hidden = !self.timeTableView.hidden;
            self.areaTableView.hidden = YES;
            self.schoolTableView.hidden = YES;
            //self.timeTableView.hidden = YES;
            break;
    }
    
    //使cell的线条充满
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)] || [self.areaTableView respondsToSelector:@selector(setSeparatorInset:)] || [self.schoolTableView respondsToSelector:@selector(setSeparatorInset:)] || [self.timeTableView respondsToSelector:@selector(setSeparatorInset:)] || [self.searchTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        [self.schoolTableView setSeparatorInset:UIEdgeInsetsZero];
        [self.areaTableView setSeparatorInset:UIEdgeInsetsZero];
        [self.timeTableView setSeparatorInset:UIEdgeInsetsZero];
        [self.searchTableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)] || [self.areaTableView respondsToSelector:@selector(setLayoutMargins:)] || [self.schoolTableView respondsToSelector:@selector(setLayoutMargins:)] || [self.searchTableView respondsToSelector:@selector(setLayoutMargins:)] || [self.timeTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        [self.schoolTableView setLayoutMargins:UIEdgeInsetsZero];
        [self.areaTableView setLayoutMargins:UIEdgeInsetsZero];
        [self.timeTableView setLayoutMargins:UIEdgeInsetsZero];
        [self.searchTableView setLayoutMargins:UIEdgeInsetsZero];
        
    }

}

#pragma mark 获取区域选择列表信息
-(void)fetchAreaFilters{
    
    CGDataCenter *dataCenter = [CGDataCenter sharedDataCenter];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"farea"] = dataCenter.selectedCity;
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/filteringContext/getArea.app" parameters:parameters success:^(id responseData) {
        
        NSLog(@"区域选择信息请求成功!!");
        //NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        
        //在前面添加一个取消选择项
        CGArea *cancleItem = [CGArea new];
        cancleItem.area = @"取消选择";
        NSMutableArray *mArray = [NSMutableArray arrayWithObject:cancleItem];
        NSArray *areas = [CGArea objectArrayWithJSONData:responseData];
        [mArray addObjectsFromArray:areas];
        
        self.areaFilters = mArray;
        [self.areaTableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"区域选择信息请求失败:%@",error);
    }];
    
}

#pragma mark 获取高校选择列表信息
-(void)fetchSchoolFilters{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    CGDataCenter *dataCenter = [CGDataCenter sharedDataCenter];
    parameters[@"city"] = dataCenter.selectedCity;
    if (self.selectedArea.length != 0) {     //如果不选择区域，默认发送当前城市的所有高校
        parameters[@"area"] = self.selectedArea;
    }
    parameters[@"page"] = @"0";
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/filteringContext/getSchoolName.app" parameters:parameters success:^(id responseData) {
        
        NSLog(@"高校选择信息请求成功!!");
        NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        NSLog(@"result:%@",result);
        //在前面添加一个取消选择项
        CGFilterSchool *cancleItem = [CGFilterSchool new];
        cancleItem.context = @"取消选择";
        NSMutableArray *mArray = [NSMutableArray arrayWithObject:cancleItem];
        NSArray *schools = [CGFilterSchool objectArrayWithJSONData:responseData];
        [mArray addObjectsFromArray:schools];
        
        self.schoolFilters = mArray;
        [self.schoolTableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"高校选择信息请求失败:%@",error);
    }];
    
}

#pragma mark 下拉刷新
-(void)loadNewStatus{
    
    CGDataCenter *dataCenter = [CGDataCenter sharedDataCenter];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"model"] = @"1";
    parameters[@"pageSize"] = [NSString stringWithFormat:@"%d",(int)SCREEN_HEIGHT/100];
    parameters[@"city"] = dataCenter.selectedCity;
    
    if (!self.products.count) {
        parameters[@"page"] = @"0";   //第一次加载最新的数据
    }else{
        CGInfoSchool *company = [self.products firstObject];
        parameters[@"uid"] = [NSString stringWithFormat:@"%d",company.uid];
    }
    
    //检查网络是否可用
    if (![ZYHRequest NetWorkIsOK]) {
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络不可用"];
        // 结束下拉刷新
        [self.tableView headerEndRefreshing];
        return;
    }
    
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/school/getData.app" parameters:parameters success:^(id responseData) {
        
        NSLog(@"校园招聘信息请求成功!!");
        // 结束下拉刷新
        [self.tableView headerEndRefreshing];
        //把接收到的数组对象变成model
        NSDictionary *jsonDic =  [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        NSString *value = jsonDic[@"data"];
        NSData *valueData = [value dataUsingEncoding:NSUTF8StringEncoding];
        //NSLog(@"result:%@",value);
        NSArray *schoolArr = [CGInfoSchool objectArrayWithJSONData:valueData];
        // 把最新的招聘信息插入到最前面
        NSIndexSet *schIndex = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, schoolArr.count)];
        [self.products insertObjects:schoolArr atIndexes:schIndex];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"学校招聘信息请求失败:%@",error);
    }];
}


#pragma mark 上拉加载
-(void)loadMoreStatus{
    
    CGDataCenter *dataCenter = [CGDataCenter sharedDataCenter];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"model"] = @"1";
    parameters[@"pageSize"] = [NSString stringWithFormat:@"%d",(int)SCREEN_HEIGHT/100];
    parameters[@"city"] = dataCenter.selectedCity;
    
    if (!self.products.count) {      //有数据才需要加载
        // 结束上拉加载
        [self.tableView footerEndRefreshing];
        return;
    }else{
        parameters[@"page"] = [NSString stringWithFormat:@"%lu",(unsigned long)self.products.count];
    }
    
    //检查网络是否可用
    if (![ZYHRequest NetWorkIsOK]) {
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络不可用"];
        // 结束上拉加载
        [self.tableView footerEndRefreshing];
        return;
    }
    
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/school/getData.app" parameters:parameters success:^(id responseData) {
        
        NSLog(@"学校招聘信息加载请求成功!!");
        // 结束上拉加载
        [self.tableView footerEndRefreshing];
        //把接收到的数组对象变成model
        NSDictionary *jsonDic =  [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        NSString *value = jsonDic[@"data"];
        NSData *valueData = [value dataUsingEncoding:NSUTF8StringEncoding];
        //NSLog(@"result:%@",value);
        NSArray *dicArray = [CGInfoSchool objectArrayWithJSONData:valueData];
        //把数组中的元素添加进去
        [self.products addObjectsFromArray:dicArray];
        // 刷新表格
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"学校招聘信息加载请求失败:%@",error);
    }];
    
}

#pragma mark overide 表格的代理方法实现
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == self.tableView) {
        return self.products.count;
    }else if (tableView == self.areaTableView){
        return self.areaFilters.count;
    }else if (tableView == self.schoolTableView){
        return self.schoolFilters.count;
    }else if (tableView == self.timeTableView){
       return self.timeFilters.count;
    }else{
        return self.searchResults.count;
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.tableView) {
        
        CGSChoolJobCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
        if (cell == nil) {
            cell = [[CGSChoolJobCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
        }
        CGInfoSchool *school = self.products[indexPath.row];
        //模型转视图模型
        CGInfoSchoolFrame *schoolFrame = [CGInfoSchoolFrame new];
        schoolFrame.product = school;
        
        cell.productFrame = schoolFrame;
        return cell;
        
    }else if (tableView == self.areaTableView){
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"areaCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"areaCell"];
        }
       
            CGArea *myArea = self.areaFilters[indexPath.row];
            cell.textLabel.text = myArea.area;
            return cell;

        
    }else if (tableView == self.schoolTableView){
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"schoolCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"schoolCell"];
        }
        CGFilterSchool *schoolFilter = self.schoolFilters[indexPath.row];
        cell.textLabel.text = schoolFilter.context;
        return cell;
        
    }else if (tableView == self.timeTableView){
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"timeCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"timeCell"];
        }
        NSString *time = self.timeFilters[indexPath.row];
        cell.textLabel.text = time;
        return cell;
        
    }else{
        
        CGSChoolJobCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchCell"];
        if (cell == nil) {
            cell = [[CGSChoolJobCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"searchCell"];
        }
        CGInfoSchool *school = self.searchResults[indexPath.row];
        //模型转视图模型
        CGInfoSchoolFrame *schoolFrame = [CGInfoSchoolFrame new];
        schoolFrame.product = school;
        cell.productFrame = schoolFrame;
        return cell;
    }

}

//设置单元格高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.tableView) {
        CGInfoSchool *school = self.products[indexPath.row];
        //模型转视图模型
        CGInfoSchoolFrame *schoolFrame = [CGInfoSchoolFrame new];
        schoolFrame.product = school;
        return schoolFrame.cellHeight;
        
    }else if (tableView == self.searchTableView) {
        CGInfoSchool *school = self.searchResults[indexPath.row];
        //模型转视图模型
        CGInfoSchoolFrame *schoolFrame = [CGInfoSchoolFrame new];
        schoolFrame.product = school;
        return schoolFrame.cellHeight;
    
    }else{
        
        return 44;
    }
}

#pragma mark 单元格选中事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //跳转到职位详情页
    if (tableView == self.tableView) {
        
        CGInfoSchool *mySchool = self.products[indexPath.row];
        CGSchoolJobDetailViewController *schoolDetailVC = [CGSchoolJobDetailViewController new];
        schoolDetailVC.mySchoolInfo = mySchool;
        schoolDetailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:schoolDetailVC animated:YES];
        return;
    }
    
    if (tableView == self.searchTableView) {
        
        CGInfoSchool *mySchool = self.searchResults[indexPath.row];
        CGSchoolJobDetailViewController *schoolDetailVC = [CGSchoolJobDetailViewController new];
        schoolDetailVC.mySchoolInfo = mySchool;
        schoolDetailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:schoolDetailVC animated:YES];
        return;
    }

    
    //区域选择Button，修改标题
    if (tableView == self.areaTableView) {
        CGArea *myArea = self.areaFilters[indexPath.row];
        if ([myArea.area isEqualToString:@"取消选择"]) {
            [self.areaButton setTitle:@"区域选择" forState:UIControlStateNormal];
            self.areaCancle = YES;
            self.selectedArea = nil;
            
        }else{
            [self.areaButton setTitle:myArea.area forState:UIControlStateNormal];
            [self.timeButton setTitle:@"招聘时间" forState:UIControlStateNormal];
            self.areaCancle = NO;
            self.timeCancle = YES;
            self.selectedArea = myArea.area;
        }
        [self.schoolButton setTitle:@"高校选择" forState:UIControlStateNormal];
        self.schoolCancle = YES;
        self.areaTableView.hidden = YES;
    }
    
    //高校选择Button，修改标题
    if (tableView == self.schoolTableView) {
        CGFilterSchool *schoolFilter = self.schoolFilters[indexPath.row];
        if ([schoolFilter.context isEqualToString:@"取消选择"]) {
            [self.schoolButton setTitle:@"高校选择" forState:UIControlStateNormal];
            self.schoolCancle = YES;
            
        }else{
            [self.schoolButton setTitle:schoolFilter.context forState:UIControlStateNormal];
            [self.timeButton setTitle:@"招聘时间" forState:UIControlStateNormal];
            self.schoolCancle = NO;
            self.timeCancle = YES;
        }
        self.schoolTableView.hidden = YES;
    }
    
    //招聘时间Button，修改标题
    if (tableView == self.timeTableView) {
        NSString *date = self.timeFilters[indexPath.row];
        if ([date isEqualToString:@"取消选择"]) {
            [self.timeButton setTitle:@"招聘时间" forState:UIControlStateNormal];
            self.timeCancle = YES;
            
        }else{
           [self.timeButton setTitle:date forState:UIControlStateNormal];
           [self.areaButton setTitle:@"区域选择" forState:UIControlStateNormal];
           [self.schoolButton setTitle:@"高校选择" forState:UIControlStateNormal];
            self.areaCancle = YES;
            self.schoolCancle = YES;
            self.selectedArea = nil;
        }
       self.timeTableView.hidden = YES;
    }
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mine_checked_icon"]];
    
    if (tableView != self.tableView && tableView != self.searchTableView) {
        
        //设置单元格为单选样式
        NSArray *array = [tableView visibleCells];
        for (UITableViewCell *cell in array) {
            cell.accessoryView = nil;
        }
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryView = imageView;
        
        [self fetchFilterResult:tableView andTableViewCell:cell];
        
    }

}

#pragma mark 条件筛选搜索结果
-(void)fetchFilterResult:(UITableView *)currentTableView andTableViewCell:(UITableViewCell *)selectedCell{
    
    NSString *filter = nil;
    NSString *title = selectedCell.textLabel.text;
    
    if (currentTableView == self.areaTableView) {
        
        if ([title isEqualToString:@"取消选择"] && self.schoolCancle && self.timeCancle) {
            self.searchTableView.hidden = YES;
            return;
        }
        
        if ([title isEqualToString:@"取消选择"]) {
            return;
        }
        
        filter = @"filter1";
        [self.areaButton setTitle:title forState:UIControlStateNormal];
        [self.schoolButton setTitle:@"高校选择" forState:UIControlStateNormal];
        [self.timeButton setTitle:@"招聘时间" forState:UIControlStateNormal];
        self.areaCancle = NO;
        self.schoolCancle = YES;
        self.timeCancle = YES;
        
    }else if (currentTableView == self.schoolTableView){
        
        if ([title isEqualToString:@"取消选择"] && self.areaCancle && self.timeCancle) {
            self.searchTableView.hidden = YES;
            return;
        }

        if ([title isEqualToString:@"取消选择"]) {
            return;
        }
        
        filter = @"filter2";
        [self.schoolButton setTitle:title forState:UIControlStateNormal];
        [self.timeButton setTitle:@"招聘时间" forState:UIControlStateNormal];
        self.schoolCancle = NO;
        self.timeCancle = YES;

    }else{
        
        if ([title isEqualToString:@"取消选择"] && self.schoolCancle && self.areaCancle) {
            self.searchTableView.hidden = YES;
            return;
        }
        
        if ([title isEqualToString:@"取消选择"]) {
            return;
        }
        
        filter = @"filter3";
        [self.timeButton setTitle:title forState:UIControlStateNormal];
        [self.areaButton setTitle:@"区域选择" forState:UIControlStateNormal];
        [self.schoolButton setTitle:@"高校选择" forState:UIControlStateNormal];
        self.timeCancle = NO;
        self.areaCancle = YES;
        self.schoolCancle = YES;

    }
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    CGDataCenter *dataCenter = [CGDataCenter sharedDataCenter];
    parameters[@"city"] = dataCenter.selectedCity;
    parameters[filter] = title;
    parameters[@"pageSize"] = [NSString stringWithFormat:@"%d",(int)SCREEN_HEIGHT/100];
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/school/getData.app" parameters:parameters success:^(id responseData) {
        
        NSLog(@"学校信息筛选请求成功!!");
        NSDictionary *jsonDic =  [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        NSString *value = jsonDic[@"data"];
        if (value) {
            NSData *valueData = [value dataUsingEncoding:NSUTF8StringEncoding];
            self.searchResults = [CGInfoSchool objectArrayWithJSONData:valueData];
        }else{
            self.searchResults = nil;
        }
        [self.searchTableView reloadData];
        currentTableView.hidden = YES;
        self.searchTableView.hidden = NO;
        NSLog(@"result:%@",value);
        
    } failure:^(NSError *error) {
        
        NSLog(@"学校信息筛选请求失败:%@",error);
    }];
    
    //清除筛选条件
    [self initAreaTableView];
    [self initSchoolTableView];
    [self initTimeTableView];
    
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
    self.areaTableView.hidden = YES;
    self.schoolTableView.hidden = YES;
    self.timeTableView.hidden = YES;
    self.searchTableView.hidden = YES;
    
    self.areaButton.selected = NO;
    self.schoolButton.selected = NO;
    self.timeButton.selected = NO;
    
}

//使cell的线条充满
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}


@end
